﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * CLR版本：4.0.30319.42000
 * 文件名：Kernel
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/2/15 16:16:40
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using ImageK.Java;
using ImageK.Measure;
using ImageK.Plugin.Filter;
using ImageK.Process;
using Microsoft.VisualBasic.Logging;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageK.Gui
{
    /// <summary>
    /// This class is an extended ImageWindow that displays histograms.
    /// </summary>
    public class HistogramWindow : ImageWindow 
    {
	    private static double SCALE = HistogramPlot.SCALE;
        static int HIST_WIDTH = HistogramPlot.HIST_WIDTH;
        static int HIST_HEIGHT = HistogramPlot.HIST_HEIGHT;
        static int XMARGIN = HistogramPlot.XMARGIN;
        static int YMARGIN = HistogramPlot.YMARGIN;
        static int WIN_WIDTH = HistogramPlot.WIN_WIDTH;
        static int WIN_HEIGHT = HistogramPlot.WIN_HEIGHT;
        static int BAR_HEIGHT = HistogramPlot.BAR_HEIGHT;

        const int INTENSITY1 = 0, INTENSITY2 = 1, RGB = 2, RED = 3, GREEN = 4, BLUE = 5;

        protected ImageStatistics stats;
        protected long[] histogram;
        protected LookUpTable lut;
        protected Rectangle frame = Rectangle.Empty;
        protected Button list, save, copy, log, live, rgb;
        protected Label value, count;
        protected static String defaultDirectory = null;
        protected int decimalPlaces;
        protected int digits;
        protected long newMaxCount;
        protected int plotScale = 1;
        protected bool logScale;
        protected Calibration cal;
        protected int yMax;
        public static int nBins = 256;

        private int srcImageID;         // ID of source image
        private ImagePlus srcImp;       // source image for live histograms
        private Thread bgThread;        // thread background drawing
        private bool doUpdate;   // tells background thread to update
        private int rgbMode = -1;
        private string blankLabel;
        private bool stackHistogram;
        // private Font font = new Font("SansSerif", Font.PLAIN, (int)(12*SCALE));
        Font font = new Font(new FontFamily("Arial"), (float)(12*SCALE), FontStyle.Regular);
        private bool showBins;
        private int col1, col2, row1, row2, row3, row4, row5;

        private object _lockObj = new object();

        public HistogramWindow(HistogramPlot plot, ImagePlus srcImp):base(plot)
        {
            srcImageID = srcImp.getID();
            frame = plot.frame;
            histogram = plot.histogram;
            cal = plot.cal;
            stats = plot.stats;
            yMax = plot.yMax;
            newMaxCount = plot.newMaxCount;
            showBins = plot.showBins;
            rgbMode = plot.rgbMode;
            col1=plot.col1; col2=plot.col2; row1=plot.row1; row2=plot.row2;
            row3=plot.row3; row4=plot.row4; row5=plot.row5;
            if (list==null)
                setup(srcImp);
        }

        /** Displays a histogram using the title "Histogram of ImageName". */
        public HistogramWindow(ImagePlus imp):base(NewImage.createRGBImage("Histogram of "+imp.getShortTitle(), WIN_WIDTH, WIN_HEIGHT, 1, NewImage.FILL_WHITE))
        {
            showHistogram(imp, 256, 0.0, 0.0);
        }

        /** Displays a histogram using the specified title and number of bins. 
		    Currently, the number of bins must be 256 expect for 32 bit images. */
        public HistogramWindow(string title, ImagePlus imp, int bins): base(NewImage.createRGBImage(title, WIN_WIDTH, WIN_HEIGHT, 1, NewImage.FILL_WHITE))
        {
            showHistogram(imp, bins, 0.0, 0.0);
        }

        /** Displays a histogram using the specified title, number of bins and histogram range.
		    Currently, the number of bins must be 256 and the histogram range range must be the 
		    same as the image range expect for 32 bit images. */
        public HistogramWindow(string title, ImagePlus imp, int bins, double histMin, double histMax): base(NewImage.createRGBImage(title, WIN_WIDTH, WIN_HEIGHT, 1, NewImage.FILL_WHITE))
        {
            showHistogram(imp, bins, histMin, histMax);
        }

        /** Displays a histogram using the specified title, number of bins, histogram range and yMax. */
        public HistogramWindow(String title, ImagePlus imp, int bins, double histMin, double histMax, int yMax): base(NewImage.createRGBImage(title, WIN_WIDTH, WIN_HEIGHT, 1, NewImage.FILL_WHITE))
        {
            this.yMax = yMax;
            showHistogram(imp, bins, histMin, histMax);
        }

        /** Displays a histogram using the specified title and ImageStatistics. */
        public HistogramWindow(String title, ImagePlus imp, ImageStatistics stats): base(NewImage.createRGBImage(title, WIN_WIDTH, WIN_HEIGHT, 1, NewImage.FILL_WHITE))
        {
            this.yMax = stats.histYMax;
            showHistogram(imp, stats);
        }

        /** Draws the histogram using the specified title and number of bins.
		    Currently, the number of bins must be 256 expect for 32 bit images. */
        public void showHistogram(ImagePlus imp, int bins)
        {
            showHistogram(imp, bins, 0.0, 0.0);
        }

        /** Draws the histogram using the specified title, number of bins and histogram range.
		    Currently, the number of bins must be 256 and the histogram range range must be 
		    the same as the image range expect for 32 bit images. */
        public void showHistogram(ImagePlus imp, int bins, double histMin, double histMax)
        {
            bool limitToThreshold = (Analyzer.getMeasurements()&Measurements.LIMIT)!=0;
            ImageProcessor ip = imp.getProcessor();
            if (ip.getMinThreshold()!=ImageProcessor.NO_THRESHOLD
            && ip.getLutUpdateMode()==ImageProcessor.NO_LUT_UPDATE)
                limitToThreshold = false;  // ignore invisible thresholds
            if (imp.isRGB() && rgbMode<INTENSITY1)
                rgbMode=INTENSITY1;
            if (rgbMode==RED||rgbMode==GREEN||rgbMode==BLUE)
            {
                int channel = rgbMode - 2;
                ColorProcessor cp = (ColorProcessor)imp.getProcessor();
                ip = cp.getChannel(channel, null);
                ImagePlus imp2 = new ImagePlus("", ip);
                imp2.setRoi(imp.getRoi());
                stats = imp2.getStatistics(Measurements.AREA+Measurements.MEAN+Measurements.MODE+Measurements.MIN_MAX, bins, histMin, histMax);
            }
            else if (rgbMode==RGB)
                stats = RGBHistogram(imp, bins, histMin, histMax);
            else
                stats = imp.getStatistics(Measurements.AREA+Measurements.MEAN+Measurements.MODE+Measurements.MIN_MAX+(limitToThreshold ? Measurements.LIMIT : 0), bins, histMin, histMax);
            showHistogram(imp, stats);
        }

        private ImageStatistics RGBHistogram(ImagePlus imp, int bins, double histMin, double histMax)
        {
            ImageProcessor ip = (ColorProcessor)imp.getProcessor();
            ip = ip.crop();
            int w = ip.getWidth();
            int h = ip.getHeight();
            ImageProcessor ip2 = new ByteProcessor(w*3, h);
            ByteProcessor temp = null;
            for (int i = 0; i<3; i++)
            {
                temp = ((ColorProcessor)ip).getChannel(i+1, temp);
                ip2.insert(temp, i*w, 0);
            }
            ImagePlus imp2 = new ImagePlus("imp2", ip2);
            return imp2.getStatistics(Measurements.AREA+Measurements.MEAN+Measurements.MODE+Measurements.MIN_MAX, bins, histMin, histMax);
        }

        /** Draws the histogram using the specified title and ImageStatistics. */
        public void showHistogram(ImagePlus srcImp, ImageStatistics stats)
        {
            if (srcImp.isRGB() && rgbMode<INTENSITY1)
                rgbMode=INTENSITY1;
            stackHistogram = stats.stackStatistics;
            if (list==null)
                setup(srcImp);
            this.stats = stats;
            cal = srcImp.getCalibration();
            bool limitToThreshold = (Analyzer.getMeasurements()&Measurements.LIMIT)!=0;
            srcImp.getMask();
            histogram = stats.getHistogram();
            if (limitToThreshold && histogram.Length==256)
            {
                ImageProcessor ip = srcImp.getProcessor();
                if (ip.getMinThreshold()!=ImageProcessor.NO_THRESHOLD)
                {
                    int lower = scaleDown(ip, ip.getMinThreshold());
                    int upper = scaleDown(ip, ip.getMaxThreshold());
                    for (int i = 0; i<lower; i++)
                        histogram[i] = 0L;
                    for (int i = upper+1; i<256; i++)
                        histogram[i] = 0L;
                }
            }
            lut = srcImp.createLut();
            int type = srcImp.getType();
            bool fixedRange = type==ImagePlus.GRAY8 || type==ImagePlus.COLOR_256 || srcImp.isRGB();
            if (imp==null)
            {
                IJ.ShowStatus("imp==null");
                return;
            }
            ImageProcessor ipNew = imp.getProcessor();
            ipNew.setColor(Color.White);
            ipNew.resetRoi();
            ipNew.fill();
            ImageProcessor srcIP = srcImp.getProcessor();
            drawHistogram(srcImp, ipNew, fixedRange, stats.histMin, stats.histMax);
            imp.updateAndDraw();
        }

        private void setup(ImagePlus imp)
        {
            bool isRGB = imp.isRGB();
            FlowLayoutPanel buttons = new FlowLayoutPanel();
            int hgap = IJ.isMacOSX()||isRGB ? 1 : 5;
            buttons.FlowDirection = FlowDirection.RightToLeft;
            // buttons.setLayout(new FlowLayout(FlowLayout.RIGHT, hgap, 0));
            // int trim = IJ.isMacOSX() ? 6 : 0;
            list = new Button()
            {
                Text = "List"
            };
            list.Click += actionPerformed;
            buttons.Controls.Add(list);
            copy = new Button()
            {
                Text = "Copy"
            };
            copy.Click += actionPerformed;
            buttons.Controls.Add(copy);
            log = new Button()
            {
                Text = "Log"
            };
            log.Click += actionPerformed;
            buttons.Controls.Add(log);
            if (!stackHistogram)
            {
                live = new Button()
                {
                    Text = "Live"
                };
                live.Click += actionPerformed;
                buttons.Controls.Add(live);
            }
            if (imp!=null && isRGB && !stackHistogram)
            {
                rgb = new Button()
                {
                    Text = "RGB"
                };
                rgb.Click += actionPerformed;
                buttons.Controls.Add(rgb);
            }
            value = new Label()
            {
                Text = " "
            };
            count = new Label()
            {
                Text = " "
            };
            add(buttons);
            GUI.scale(buttons);
            //todo:
            // pack();
            // if (IJ.isMacOSX() && IJ.isJava18())
            // {
            //     IJ.wait(50);
            //     pack();
            // }
        }

        public void setup()
        {
            setup(null);
        }

        public void mouseMoved(int x, int y)
        {
            ImageProcessor ip = this.imp!=null ? this.imp.getProcessor() : null;
            if (ip==null)
                return;
            if ((frame!=null)  && x>=frame.X && x<=(frame.X+frame.Width))
            {
                x = (x - frame.X);
                int index = (int)(x*(SCALE*histogram.Length)/HIST_WIDTH/SCALE);
                if (index>=histogram.Length) index = histogram.Length-1;
                double value = cal.getCValue(stats.histMin+index*stats.binSize);
                drawValueAndCount(ip, value, histogram[index]);
            }
            else
                drawValueAndCount(ip, double.NaN, -1);
            this.imp.updateAndDraw();
        }

        protected void drawHistogram(ImageProcessor ip, bool fixedRange)
        {
            drawHistogram(null, ip, fixedRange, 0.0, 0.0);
        }

        void drawHistogram(ImagePlus imp, ImageProcessor ip, bool fixedRange, double xMin, double xMax)
        {
            int x, y;
            long maxCount2 = 0;
            int mode2 = 0;
            long saveModalCount;
            ip.setColor(Color.Black);
            ip.setLineWidth(1);
            decimalPlaces = Analyzer.getPrecision();
            digits = cal.calibrated()||stats.binSize!=1.0 ? decimalPlaces : 0;
            saveModalCount = histogram[stats.mode];
            for (int i = 0; i<histogram.Length; i++)
            {
                if ((histogram[i] > maxCount2) && (i != stats.mode))
                {
                    maxCount2 = histogram[i];
                    mode2 = i;
                }
            }
            newMaxCount = histogram[stats.mode];
            if ((newMaxCount>(maxCount2 * 2)) && (maxCount2 != 0))
                newMaxCount = (int)(maxCount2 * 1.5);
            if (logScale || IJ.shiftKeyDown() && !liveMode())
                drawLogPlot(yMax>0 ? yMax : newMaxCount, ip);
            drawPlot(yMax>0 ? yMax : newMaxCount, ip);
            histogram[stats.mode] = saveModalCount;
            x = XMARGIN + 1;
            y = YMARGIN + HIST_HEIGHT + 2;
            if (imp==null)
                lut.drawUnscaledColorBar(ip, x-1, y, HIST_WIDTH, BAR_HEIGHT);
            else
                drawAlignedColorBar(imp, xMin, xMax, ip, x-1, y, HIST_WIDTH, BAR_HEIGHT);
            y += BAR_HEIGHT+(int)(15*SCALE);
            drawText(ip, x, y, fixedRange);
            srcImageID = imp.getID();
        }

        void drawAlignedColorBar(ImagePlus imp, double xMin, double xMax, ImageProcessor ip, int x, int y, int width, int height)
        {
            ImageProcessor ipSource = imp.getProcessor();
            float[] pixels = null;
            ImageProcessor ipRamp = null;
            if (rgbMode>=INTENSITY1)
            {
                ipRamp = new FloatProcessor(width, height);
                if (rgbMode==RED)
                    ipRamp.setColorModel(LUT.createLutFromColor(Color.Red));
                else if (rgbMode==GREEN)
                    ipRamp.setColorModel(LUT.createLutFromColor(Color.Green));
                else if (rgbMode==BLUE)
                    ipRamp.setColorModel(LUT.createLutFromColor(Color.Blue));
                pixels = (float[])ipRamp.getPixels();
            }
            else
                pixels = new float[width*height];
            for (int j = 0; j<height; j++)
            {
                for (int i = 0; i<width; i++)
                    pixels[i+width*j] = (float)(xMin+i*(xMax-xMin)/(width - 1));
            }
            double min = ipSource.getMin();
            double max = ipSource.getMax();
            if (ipSource.getNChannels()==1)
            {
                ColorModel cm = null;
                if (imp.isComposite())
                {
                    if (stats!=null && stats.pixelCount>ipSource.getPixelCount())
                    { // stack histogram
                        cm = LUT.createLutFromColor(Color.White);
                        min = stats.min;
                        max = stats.max;
                    }
                    else
                        cm = ((CompositeImage)imp).getChannelLut();
                }
                else if (ipSource.getMinThreshold()==ImageProcessor.NO_THRESHOLD)
                    cm = ipSource.getColorModel();
                else
                    cm = ipSource.getCurrentColorModel();
                ipRamp = new FloatProcessor(width, height, pixels, cm);
            }
            ipRamp.setMinAndMax(min, max);
            ImageProcessor bar = null;
            if (ip is ColorProcessor)
			    bar = ipRamp.convertToRGB();
            else
                bar = ipRamp.convertToByte(true);
            ip.insert(bar, x, y);
            ip.setColor(Color.Black);
            ip.drawRect(x-1, y, width+2, height);
        }

        /** Scales a threshold level to the range 0-255. */
        int scaleDown(ImageProcessor ip, double threshold)
        {
            double min = ip.getMin();
            double max = ip.getMax();
            if (max>min)
                return (int)(((threshold-min)/(max-min))*255.0);
            else
                return 0;
        }

        void drawPlot(long maxCount, ImageProcessor ip)
        {
            if (maxCount==0) maxCount = 1;
            frame = new Rectangle(XMARGIN, YMARGIN, HIST_WIDTH, HIST_HEIGHT);
            ip.drawRect(frame.X-1, frame.Y, frame.Width+2, frame.Height+1);
            if (histogram.Length==256)
            {
                double scale2 = HIST_WIDTH/256.0;
                int barWidth = 1;
                if (SCALE>1) barWidth=2;
                if (SCALE>2) barWidth=3;
                for (int i = 0; i < 256; i++)
                {
                    int x = (int)(i*scale2);
                    int y = (int)(((double)HIST_HEIGHT*(double)histogram[i])/maxCount);
                    if (y>HIST_HEIGHT) y = HIST_HEIGHT;
                    for (int j = 0; j<barWidth; j++)
                        ip.drawLine(x+j+XMARGIN, YMARGIN+HIST_HEIGHT, x+j+XMARGIN, YMARGIN+HIST_HEIGHT-y);
                }
            }
            else if (histogram.Length<=HIST_WIDTH)
            {
                int index, y;
                for (int i = 0; i<HIST_WIDTH; i++)
                {
                    index = (int)(i*(double)histogram.Length/HIST_WIDTH);
                    y = (int)(((double)HIST_HEIGHT*(double)histogram[index])/maxCount);
                    if (y>HIST_HEIGHT) y = HIST_HEIGHT;
                    ip.drawLine(i+XMARGIN, YMARGIN+HIST_HEIGHT, i+XMARGIN, YMARGIN+HIST_HEIGHT-y);
                }
            }
            else
            {
                double xscale = (double)HIST_WIDTH/histogram.Length;
                for (int i = 0; i<histogram.Length; i++)
                {
                    long value = histogram[i];
                    if (value>0L)
                    {
                        int y = (int)(((double)HIST_HEIGHT*(double)value)/maxCount);
                        if (y>HIST_HEIGHT) y = HIST_HEIGHT;
                        int x = (int)(i*xscale)+XMARGIN;
                        ip.drawLine(x, YMARGIN+HIST_HEIGHT, x, YMARGIN+HIST_HEIGHT-y);
                    }
                }
            }
        }

        void drawLogPlot(long maxCount, ImageProcessor ip)
        {
            frame = new Rectangle(XMARGIN, YMARGIN, HIST_WIDTH, HIST_HEIGHT);
            ip.drawRect(frame.X-1, frame.Y, frame.Width+2, frame.Height+1);
            double max = Math.Log(maxCount);
            ip.setColor(Color.Gray);
            if (histogram.Length==256)
            {
                double scale2 = HIST_WIDTH/256.0;
                int barWidth = 1;
                if (SCALE>1) barWidth=2;
                if (SCALE>2) barWidth=3;
                for (int i = 0; i < 256; i++)
                {
                    int x = (int)(i*scale2);
                    int y = histogram[i]==0 ? 0 : (int)(HIST_HEIGHT*Math.Log(histogram[i])/max);
                    if (y>HIST_HEIGHT) y = HIST_HEIGHT;
                    for (int j = 0; j<barWidth; j++)
                        ip.drawLine(x+j+XMARGIN, YMARGIN+HIST_HEIGHT, x+j+XMARGIN, YMARGIN+HIST_HEIGHT-y);
                }
            }
            else if (histogram.Length<=HIST_WIDTH)
            {
                int index, y;
                for (int i = 0; i<HIST_WIDTH; i++)
                {
                    index = (int)(i*(double)histogram.Length/HIST_WIDTH);
                    y = histogram[index]==0 ? 0 : (int)(HIST_HEIGHT*Math.Log(histogram[index])/max);
                    if (y>HIST_HEIGHT) y = HIST_HEIGHT;
                    ip.drawLine(i+XMARGIN, YMARGIN+HIST_HEIGHT, i+XMARGIN, YMARGIN+HIST_HEIGHT-y);
                }
            }
            else
            {
                double xscale = (double)HIST_WIDTH/histogram.Length;
                for (int i = 0; i<histogram.Length; i++)
                {
                    long value = histogram[i];
                    if (value>0L)
                    {
                        int y = (int)(HIST_HEIGHT*Math.Log(value)/max);
                        if (y>HIST_HEIGHT) y = HIST_HEIGHT;
                        int x = (int)(i*xscale)+XMARGIN;
                        ip.drawLine(x, YMARGIN+HIST_HEIGHT, x, YMARGIN+HIST_HEIGHT-y);
                    }
                }
            }
            ip.setColor(Color.Black);
        }

        void drawText(ImageProcessor ip, int x, int y, bool fixedRange)
        {
            ip.setFont(font);
            ip.setAntialiasedText(true);
            double hmin = cal.getCValue(stats.histMin);
            double hmax = cal.getCValue(stats.histMax);
            double range = hmax-hmin;
            if (fixedRange&&!cal.calibrated()&&hmin==0&&hmax==255)
                range = 256;
            ip.drawString(d2s(hmin), x - 4, y);
            ip.drawString(d2s(hmax), x + HIST_WIDTH - getWidth(hmax, ip) + 10, y);
            if (rgbMode>=INTENSITY1)
            {
                x += HIST_WIDTH/2;
                y += 1;
                ip.setJustification(ImageProcessor.CENTER_JUSTIFY);
                bool weighted = ((ColorProcessor)ip).weightedHistogram();
                switch (rgbMode)
                {
                    case INTENSITY1: ip.drawString((weighted ? "Intensity (weighted)" : "Intensity (unweighted)"), x, y); break;
                    case INTENSITY2: ip.drawString((weighted ? "Intensity (unweighted)" : "Intensity (weighted)"), x, y); break;
                    case RGB: ip.drawString("R+G+B", x, y); break;
                    case RED: ip.drawString("Red", x, y); break;
                    case GREEN: ip.drawString("Green", x, y); break;
                    case BLUE: ip.drawString("Blue", x, y); break;
                }
                ip.setJustification(ImageProcessor.LEFT_JUSTIFY);
            }
            double binWidth = range/stats.nBins;
            binWidth = Math.Abs(binWidth);
            showBins = binWidth!=1.0 || !fixedRange;
            col1 = XMARGIN + 5;
            col2 = XMARGIN + HIST_WIDTH/2;
            row1 = y+(int)(25*SCALE);
            if (showBins) row1 -= (int)(8*SCALE);
            row2 = row1 + (int)(15*SCALE);
            row3 = row2 + (int)(15*SCALE);
            row4 = row3 + (int)(15*SCALE);
            row5 = row4 + (int)(15*SCALE);
            long count = stats.longPixelCount>0 ? stats.longPixelCount : stats.pixelCount;
            string modeCount = " (" + stats.maxCount + ")";
            if (modeCount.Length>12) modeCount = "";

            ip.drawString("N: " + count, col1, row1);
            ip.drawString("Min: " + d2s(stats.min), col2, row1);
            ip.drawString("Mean: " + d2s(stats.mean), col1, row2);
            ip.drawString("Max: " + d2s(stats.max), col2, row2);
            ip.drawString("StdDev: " + d2s(stats.stdDev), col1, row3);
            ip.drawString("Mode: " + d2s(stats.dmode) + modeCount, col2, row3);
            if (showBins)
            {
                ip.drawString("Bins: " + d2s(stats.nBins), col1, row4);
                ip.drawString("Bin Width: " + d2s(binWidth), col2, row4);
            }
            drawValueAndCount(ip, Double.NaN, -1);
        }

        private void drawValueAndCount(ImageProcessor ip, double value, long count)
        {
            int y = showBins ? row4 : row3;
            ip.setRoi(0, y, WIN_WIDTH, WIN_HEIGHT-y);
            ip.setColor(Color.White);
            ip.fill();
            ip.setColor(Color.Black);
            string sValue = double.IsNaN(value) ? "---" : d2s(value);
            string sCount = count==-1 ? "---" : ""+count;
            int row = showBins ? row5 : row4;
            ip.drawString("Value: " + sValue, col1, row);
            ip.drawString("Count: " + sCount, col2, row);
        }

        private String d2s(double d)
        {
            if ((int)d==d)
                return IJ.d2s(d, 0);
            else
                return IJ.d2s(d, 3, 8);
        }

        int getWidth(double d, ImageProcessor ip)
        {
            return ip.getStringWidth(d2s(d));
        }

        /** Returns the histogram values as a ResultsTable. */
        public ResultsTable getResultsTable()
        {
            int decimalPlaces = Analyzer.getPrecision();
            if (digits==0 && stats.binSize!=1.0)
                digits = decimalPlaces;
            ResultsTable rt = new ResultsTable();
            rt.setPrecision(digits);
            String vheading = stats.binSize==1.0 ? "value" : "bin start";
            if (cal.calibrated() && !cal.isSigned16Bit())
            {
                for (int i = 0; i<stats.nBins; i++)
                {
                    rt.setValue("level", i, i);
                    rt.setValue(vheading, i, cal.getCValue(stats.histMin+i*stats.binSize));
                    rt.setValue("count", i, histogram[i]);
                }
            }
            else
            {
                for (int i = 0; i<stats.nBins; i++)
                {
                    if (stats.binSize!=1.0)
                        rt.setValue("index", i, i);
                    rt.setValue(vheading, i, cal.getCValue(stats.histMin+i*stats.binSize));
                    rt.setValue("count", i, histogram[i]);
                }
            }
            return rt;
        }

        protected void showList()
        {
            ResultsTable rt = getResultsTable();
            rt.show(this.Text);
        }

        protected void copyToClipboard()
        {
            //todo:
            // Clipboard systemClipboard = null;
            // try { systemClipboard = getToolkit().getSystemClipboard(); }
            // catch (Exception e) { systemClipboard = null; }
            // if (systemClipboard==null)
            // { IJ.error("Unable to copy to Clipboard."); return; }
            // IJ.ShowStatus("Copying histogram values...");
            // CharArrayWriter aw = new CharArrayWriter(stats.nBins*4);
            // PrintWriter pw = new PrintWriter(aw);
            // for (int i = 0; i<stats.nBins; i++)
            //     pw.print(ResultsTable.d2s(cal.getCValue(stats.histMin+i*stats.binSize), digits)+"\t"+histogram[i]+"\n");
            // String text = aw.toString();
            // pw.close();
            // StringSelection contents = new StringSelection(text);
            // systemClipboard.setContents(contents, this);
            // IJ.showStatus(text.length() + " characters copied to Clipboard");
        }

        void replot()
        {
            ImageProcessor ip = this.imp.getProcessor();
            frame = new Rectangle(XMARGIN, YMARGIN, HIST_WIDTH, HIST_HEIGHT);
            ip.setColor(Color.White);
            ip.setRoi(frame.X-1, frame.Y, frame.Width+2, frame.Height);
            ip.fill();
            ip.resetRoi();
            ip.setColor(Color.Black);
            ip.setLineWidth(1);
            if (logScale)
            {
                drawLogPlot(yMax>0 ? yMax : newMaxCount, ip);
                drawPlot(yMax>0 ? yMax : newMaxCount, ip);
            }
            else
                drawPlot(yMax>0 ? yMax : newMaxCount, ip);
            this.imp.updateAndDraw();
        }

        public void actionPerformed(object? sender, EventArgs e)
        {
            object b = sender;
            if (b==live)
                toggleLiveMode();
            else if (b==rgb)
                changeChannel();
            else if (b==list)
                showList();
            else if (b==copy)
                copyToClipboard();
            else if (b==log)
            {
                logScale = !logScale;
                replot();
            }
        }

        // public void lostOwnership(Clipboard clipboard, Transferable contents) { }

        public int[] getHistogram()
        {
            int[] hist = new int[histogram.Length];
            for (int i = 0; i<histogram.Length; i++)
                hist[i] = (int)histogram[i];
            return hist;
        }

        public double[] getXValues()
        {
            double[] values = new double[stats.nBins];
            for (int i = 0; i<stats.nBins; i++)
                values[i] = cal.getCValue(stats.histMin+i*stats.binSize);
            return values;
        }

        private void toggleLiveMode()
        {
            if (liveMode())
                removeListeners();
            else
                enableLiveMode();
        }

        private void changeChannel()
        {
            ImagePlus imp = WindowManager.getImage(srcImageID);
            if (imp==null || !imp.isRGB())
                return;
            else
            {
                rgbMode++;
                if (rgbMode>BLUE) rgbMode=INTENSITY1;
                ColorProcessor cp = (ColorProcessor)imp.getProcessor();
                bool weighted = cp.weightedHistogram();
                if (rgbMode==INTENSITY2)
                {
                    double[] weights = cp.getRGBWeights();
                    if (weighted)
                        cp.setRGBWeights(1d/3d, 1d/3d, 1d/3d);
                    else
                        cp.setRGBWeights(0.299, 0.587, 0.114);
                    showHistogram(imp, 256);
                    cp.setRGBWeights(weights);
                }
                else
                    showHistogram(imp, 256);
            }
        }

        private bool liveMode()
        {
            return live!=null && live.ForeColor==Color.Red;
        }

        private void enableLiveMode()
        {
            if (bgThread==null)
            {
                srcImp = WindowManager.getImage(srcImageID);
                if (srcImp==null) return;
                // bgThread = new Thread(this, "Live Histogram");
                // bgThread.setPriority(Math.max(bgThread.getPriority()-3, Thread.MIN_PRIORITY));
                // bgThread.start();

                bgThread = new Thread(run);
                bgThread.Start();
                imageUpdated(srcImp);
            }
            createListeners();
            if (srcImp!=null)
                imageUpdated(srcImp);
        }

        // Unused
        public void imageOpened(ImagePlus imp)
        {
        }

        // This listener is called if the source image content is changed
        public void imageUpdated(ImagePlus imp)
        {
            if (imp==srcImp)
            {
                doUpdate = true;
                Monitor.Pulse(_lockObj);
            }
        }

        public void roiModified(ImagePlus img, int id)
        {
            if (img==srcImp)
            {
                doUpdate=true;
                Monitor.Pulse(_lockObj);
            }
        }

        // If either the source image or this image are closed, exit
        public void imageClosed(ImagePlus imp)
        {
            if (imp==srcImp || imp==this.imp)
            {
                if (bgThread!=null)
                    bgThread.Interrupt();
                bgThread = null;
                removeListeners();
                srcImp = null;
            }
        }

        // the background thread for live plotting.
        public void run()
        {
            while (true)
            {
                if (doUpdate && srcImp!=null)
                {
                    if (srcImp.getRoi()!=null)
                        IJ.wait(50);    //delay to make sure the roi has been updated
                    if (srcImp!=null)
                    {
                        if (srcImp.getBitDepth()==16 && ImagePlus.getDefault16bitRange()!=0)
                            showHistogram(srcImp, 256, 0, Math.Pow(2, ImagePlus.getDefault16bitRange())-1);
                        else
                            showHistogram(srcImp, 256);
                    }
                }

                Monitor.Enter(_lockObj);
                if (doUpdate)
                {
                    doUpdate = false;       //and loop again
                }
                else
                {
                    try { Monitor.Wait(_lockObj); } //notify wakes up the thread
                    catch (ThreadInterruptedException e)
                    { //interrupted tells the thread to exit
                        return;
                    }
                }
                Monitor.Exit(_lockObj);
            }
        }

        private void createListeners()
        {
            //todo:
            if (srcImp==null)
                return;
            // ImagePlus.addImageListener(this);
            // Roi.addRoiListener(this);
            // if (live!=null)
            // {
            //     // Font font = live.getFont();
            //     // live.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));
            //     Font font = live.Font;
            //     live.Font = new Font(font.Name, font.Size, FontStyle.Bold);
            //     live.ForeColor = Color.Red;
            // }
        }

        private void removeListeners()
        {
            //todo:
            if (srcImp==null)
                return;
            // ImagePlus.removeImageListener(this);
            // Roi.removeRoiListener(this);
            // if (live!=null)
            // {
            //     Font font = live.getFont();
            //     live.setFont(new Font(font.getName(), Font.PLAIN, font.getSize()));
            //     live.setForeground(Color.black);
            // }
        }

    }
}
